set more off

********************************************************************************
* Open data
********************************************************************************

use scratchdata/ncds_combined.dta, clear



***********************************************************************************************
* Correlation between Personality and Creativity
***********************************************************************************************

pwcorr creativityt_psd_7 Cons_I50 Agre_I50  ES_I50 EX_I50  Inte_I50 WEMWB

mat C = r(C)
mat C = C[2...,1]


esttab  matrix(C,fmt("%9.3f" "%9.3f" "%9.3f" )) using results/Tables/personality.tex, replace ///
	varlabels(Cons_I50 "Conscientiousness" ///
	Agre_I50 "Agreeableness"  ///
	ES_I50 "Emotional stability" ///
	EX_I50 "Extraversion" ///
	Inte_I50  "Intellect " ///
	WEMWB_I50 "W-E mental well-being") /// 
	collabels("Creativity" )  nomtitles


***********************************************************************************************
* Correlation between skills and occupational attainment
***********************************************************************************************

preserve

local EvalSkPractical  SkE30_I33 SkE24_I33 SkE54_I33 SkE39_I33 
local EvalSkAnalyitic  SkE51_I33 SkE57_I33 SkE36_I33 
local EvalSkMixed      SkE18_I33 SkE21_I33 SkE27_I33 SkE60_I33 
local EvalSkGuiding    SkE42_I33 SkE45_I33 SkE48_I33   
local Skills `EvalSkPractical' `EvalSkAnalyitic' `EvalSkMixed' `EvalSkGuiding'

keep `Skills' SC_I* NCDSID


* reshape to long dataset
reshape long SC_I, i(NCDSID) j(age)

drop if age==23

ta SC_I, nol
recast double SC_I
replace SC_I=round(SC_I*10)
ta SC_I, gen(Occ)
su Occ*

local O Occ1 Occ2 Occ3 Occ4 Occ5 Occ6


matrix C = J(16,6,.)
ds 	`Skills'
local SkList `r(varlist) '



local i 1
foreach s in `SkList'{
	forvalues c = 1/6{
		corr `s' Occ`c'
		mat C[`i',`c'] =  `r(rho)'
	}
	local i = `i' + 1
}

mat li C

svmat C, names(col)
keep c1 - c6
drop if c1==.
ge n=_n
ta n, gen(nn)

forvalues c = 1/6{
	reg c`c' nn*, noco
	est sto es`c'
}


esttab es* using results/Tables/CorrSkillsOcc.tex, replace frag ///
	cells(b(fmt(3))) collabels(none) noobs keep (nn1 nn2 nn3 nn4) nonum nomtitles ///
	prehead( & "Professional" & "\shortstack{Managerial \\\& Technical}" &  "\shortstack{Skilled \\non-manual}" & "\shortstack{Skilled \\manual}" & "\shortstack{Partly \\skilled}" & "Unskilled" \\ \cline{2-7} \\[-.5em] ///
	\\[-.15em]  \multicolumn{7}{l}{\textbf{Panel A: Primarily practical skills}}\\ & \\[-.75em]) ///
	posthead(" ") ///
	coeflabels(nn1 "Construction \& assembly" ///
	nn2 "Using tools properly" ///
	nn3 "Selling products or services" ///
	nn4 "Looking after people who need care") 

esttab es* using results/Tables/CorrSkillsOcc.tex, append frag ///
	cells(b(fmt(3))) collabels(none) noobs keep (nn5 nn6 nn7 ) nonum nomtitles ///
	prehead(\\[-.15em] \multicolumn{7}{l}{\textbf{Panel B: Primarily analytical skills}} \\ & \\[-.75em] ) ///
	posthead(" ") ///
	coeflabels(nn5 "Mathematical calculations" ///
	nn6 "Understanding finance" ///
	nn7 "Using a computer to solve problems") 

esttab es* using results/Tables/CorrSkillsOcc.tex, append frag ///
	cells(b(fmt(3))) collabels(none) noobs keep (nn8 nn9 nn10 nn11) nonum nomtitles ///
	prehead(\\[-.15em] \multicolumn{7}{l}{\textbf{Panel C: Mixed skills}} \\ & \\[-.75em] ) ///
	posthead(" ") ///
	coeflabels(nn8 "Writing clearly" ///
	nn9 "Speaking clearly" ///
	nn10 "Reading plans or diagrams" ///
	nn11 "Running an organisation") 	
	
esttab es* using results/Tables/CorrSkillsOcc.tex, append frag ///
	cells(b(fmt(3))) collabels(none) noobs keep (nn12 nn13 nn14) nonum nomtitles ///
	prehead(\\[-.15em]  \multicolumn{7}{l}{\textbf{Panel D: Guiding others}} \\ & \\[-.75em] ) ///
	posthead(" ") ///
	coeflabels(nn12 "Advising" ///
	nn13 "Teaching" ///
	nn14 "Supervising") ///
	postfoot(\\ \hline \hline)
	

restore

***********************************************************************************************
* Math and Engligh entry for age 16 quals
***********************************************************************************************

// Generate summary statistics for outcomes 
estpost summarize  EEnglighCSE EEnglighOL EEnglishAny EMathCSE EMathOL  EMathAny 

esttab using  results/Tables/ExamEntry.tex, cells("mean(fmt(3))") nonumber noobs  wide replace  nodepvars collabels(none) ///
coeflabels(EEnglighCSE "English CSE entry" EEnglighOL "English O-level entry" EEnglishAny "English CSE and/or O-level entry" ///
 EMathCSE  "Math CSE entry"  EMathOL "Math O-level entry" EMathAny "Math CSE and/or O-level entry" ) mtitles("Exam entry rates") nodepv


********************************************************************************
* Cognitive tests at ages 7 and 11 years: Descriptive statistics factor loadings on cognitive ability.
********************************************************************************

factor math_7 reading_7 math_11 reading_11 generalab_11, factor(1) 

mat define S=J(5,7,.)
local Q math_7 reading_7 math_11 reading_11 generalab_11 

local i=1
foreach q of local Q{
	su `q'
	mat S[`i',1]=r(mean)
	mat S[`i',2]=r(sd)
	mat S[`i',3]=r(min)
	mat S[`i',4]=r(max)
	mat S[`i',5]=r(N)
	mat S[`i',6]=FactorLoadings[`i',1]
	mat S[`i',7]=Eigenvalues[1,`i']

	local i=`i'+1
}


esttab  matrix(S,fmt("%9.2f" "%9.2f" 0 0 "%9.0fc" 3)) using results/Tables/Tests.tex  , replace ///
	nomtitles ///
	varlabels(r1 "Math at age 7 years" ///
	r2 "Reading at age 7 years" ///
	r3 "Math at age 11 years" ///
	r4 "Reading at age 11 years" ///
	r5 "General ability at age 11 years") nonumber ///
	collabels("Mean" "Std Dev." "Min." "Max." "Observations" "loading" "value") ///
	prehead( & \multicolumn{5}{c}{Descriptive statistics} & Factor & Eigen- \\ \cline{2-7} ) ///
	posthead(\cline{2-7}) frag


********************************************************************************
* creativity: Distribution of teachers' ratings of children's creativity
********************************************************************************

preserve

* non-reverse coded creastivity
ge nrc =6-creativityt_r_7

contract nrc creativityt_r_7
egen Obspercent= total(_freq)
replace Obspercent= (_freq/ Obspercent)*100

ge Benchmark = 5 if (creativityt_r_7==1 | creativityt_r_7==5)
replace  Benchmark = 25 if  (creativityt_r_7==2 | creativityt_r_7==4)
replace  Benchmark = 40 if  (creativityt_r_7==3)

estpost tabstat  Obspercent Benchmark, by(nrc)

esttab using results/Tables/EmpiricalDist.tex, replace cells("Benchmark(fmt(2)) Obspercent(fmt(2))  pcreativityt_p_7(fmt(2)) pcreativityt_ep_7(fmt(2))") noobs nomtitle nonumber varlabels(`e(labels)') drop(Total) varwidth(80) collab(, none) frag prehead(\begin{tabular}{ccccc} \hline \hline ///
	& \\ ///
	Creativity scale  (1 is highest)	  & Intended population percentage & Observed percentage \\) ///
	posthead ( \hline & \\ ) /// 
	postfoot( & \\   \hline \hline \end{tabular} )

restore


******************************************************************************************
* Correlation between creativity and cognitive ability
******************************************************************************************

mat m = J(2,1,.)

corr creativityt_psd_7 Cog
mat m[1,1]=r(rho)

pcorr creativityt_psd_7 $TestC $Controls 
mat c = r(p_corr)
mat m[2,1]=c["CogAbility",1]

esttab  matrix(m,fmt(3)) using results/Tables/corr.tex  , replace ///
	nomtitles collabels("") ///
	varlabels(r1 "Correlation between Cognitive Ability and creativity percentile score" ///
	r2 "Partial correlation between Cognitive Ability \& creativity  (holding fixed controls, inc evaluation error)") nonumber frag


***********************************************************************************************
* Correlation between controls and: i) creativity, ii) CA, iii) Teacher eval of Creativity
***********************************************************************************************

local controllabels refcat(motherage_0 "\shortstack{ \textbf{Controls collected at birth from the PMS questionnaire:}}" ///
	Pupattendance_7 "\shortstack[l]{ \\[.25em] \textbf{Controls collected at age 7 from a school questionnaire:} \\ \textit{Characteristics of the cohort member:}  }" ///
	Pupattendance_11 "\shortstack[l]{ \\[.25em] \textbf{Controls collected at age 11 from a school questionnaire:} \\ \textit{Characteristics of the cohort member:}  }" ///
		kidshh_11 "\shortstack[l]{ \\[.25em] \textbf{Controls collected at age 11 from a parental questionnaire:}}" ///
	StateSch_7	"\shortstack[l]{\textit{Characteristics of the cohort member's school:}}" /// 
	StateSch_11	"\shortstack[l]{\textit{Characteristics of the cohort member's school:}}" /// 
	classsize_7 "\shortstack[l]{\textit{Characteristics of the cohort member's school class:}}" /// 
	classsize_11 "\shortstack[l]{\textit{Characteristics of the cohort member's school class:}}" /// 
	Iregion_0_1 "\textit{Mother's region of residence:}" ///
	Imotherstopped_0_1 "\shortstack{\textit{Mother's working behavior during the pregnancy:}}" ///
	Imotherjob_0_1 "\textit{Mother's social class:}" ///
	Ifathersc_0_1 "\textit{Mother's husband's social class:}" ///
	Imotherfsc_0_1 "\textit{Mother's father's social class:}" ///
	kidshh_7 "\shortstack{ \\[.25em] \textbf{Controls collected at age 7 from a parental questionnaire:}}" ///
	Iregion_7_1 "\textit{Region of residence at age 7 years:}" ///
	Iparentwanted_7_0 "\shortstack{\textit{Parents want cohort member to stay in education beyond the minimum school leaving age:}}" ///
	Imotherread_7_2 "\textit{Frequency mother reads to cohort member:}" ///
	Ifatherread_7_2 "\textit{Frequency father reads to cohort member:}" ///
	Imotheroutings_7_2 "\shortstack{\textit{Frequency mother goes on outings with cohort member:}}" ///
	Ifatheroutings_7_2 "\shortstack{\textit{Frequency father goes on outings with cohort member:}}" ///
	Idadsrole_7_2 "\shortstack{\textit{Father's role in managing cohort member (relative to mother):}}" ///
	Ifathersc_7_1 "\textit{Father's social class:}" ///
	Imotherworkb4sch_7_2 "\shortstack{\textit{Mother's working behavior before cohort member started school:}}" ///
	Imotherworkafsch_7_2 "\shortstack{\textit{Mother's working behavior since cohort member started school:}}" ///
	Ihousetenure_7_2 "\textit{Housing tenure:}" ///
	Ibreatfed_7_2 "\textit{Breast feeding duration for cohort member:}" ///
	Iregion_11_1 "\textit{Region of residence at age 11 years:}" ///
	Iparentwanted_11_0 "\shortstack{\textit{Parents want cohort member to stay in education beyond the minimum school leaving age:}}" ///
	Imotheroutings_11_1 "\shortstack{\textit{Mother goes on outings with cohort member:}}" ///
	Ifatheroutings_11_1 "\shortstack{\textit{Father goes on outings with cohort member:}}" ///
	Ifathersc_11_1 "\textit{Father's social class:}" ///
	Ihousetenure_11_1 "\textit{Housing tenure:}" ///
	ISCmum_11_1 "\textit{Mother's social class:}" ///
	Imumwklastyr_11_2 "\shortstack{\textit{Mother's working behavior in the last year:}}" ///
	Ispeech_7_2 "\shortstack[l]{ \\[.25em] \textbf{Controls collected at age 7 from a medical questionnaire:} \\ \textit{Speech assessment:}}" ///
	Ihearing_7_2 "\textit{Hearing assessment:}" ///
	Ivision_7_2 "\textit{Vision assessment:}" ///
	Ispeech_11_1 "\shortstack[l]{\\[.25em] \textbf{Controls collected at age 11 from a medical questionnaire:} \\ \textit{Speech assessment:}}" ///
	Ihearing_11_1 "\\[-1em]\textit{Hearing assessment:}" ///
	Ivision_11_1 "\\[-1em]\textit{Vision assessment:}" ///
	Idadsrole_11_1 "\shortstack{\textit{Father's role in managing cohort member (relative to mother):}}", nolabel)

label var CogAbility "Cognitive ability"
label var creativityt_psd_7 "Creativity"
label var Imotherpostcompeduc_0_1 "Mother in education beyond the minimum school leaving age"
label var Imothersmoke_0_1 "Mother smoked during the pregnancy"
label var Ifatherpostcompeduc_7_1 "Father in education beyond the minimum school leaving age"
label var Imumwork_11_1 "Mother has worked since cohort member was age 7"
label var Ifinancialdiff_7_1 "Household has financial difficulties"
label var Ifinancialdiff_11_1 "Household has financial difficulties"
label var IFSM_11_1 "Household receives free school meals (at least one child)"
label var birthweigh "Cohort member's birthweight"
label var gestation_0 "Gestational duration of the pregnancy"
label var parity_0 "Mother's parity (number of previous live births or stillbirths)"
label var Imotherstopped_0_1 "\ \ \ Stopped before week 13"
label var Imotherstopped_0_2 "\ \ \ Stopped in week 13--16"
label var Imotherstopped_0_3 "\ \ \ Stopped in week 17--20"
label var Imotherstopped_0_4 "\ \ \ Stopped in week 21--24"
label var Imotherstopped_0_5 "\ \ \ Stopped in week 25--28"
label var Imotherstopped_0_6 "\ \ \ Stopped in week 29--30"
label var Imotherstopped_0_7 "\ \ \ Stopped in week 31--32"
label var Imotherstopped_0_8 "\ \ \ Stopped in week 33--34"
label var Imotherstopped_0_9 "\ \ \ Stopped in week 35--36"
label var Imotherstopped_0_10 "\ \ \ Stopped after week 36"

foreach var of varlist Pupattendance_7 ESN_7 NativeEnglish_7 StateSch_7 SchSize_7 PTA  classsize_7 SchAbility_7  pNDadSC13N pNDadSC3M4 pNDadSC5 pNkidsParTea Pupattendance_11 ESN_11  NativeEnglish_11 StateSch_11 SchSize_11 classsize_11 SchAbility_11{
	local varlabel : variable label `var'
	label var `var' "\ \ \ `varlabel'" 
}


replace motherage_0=. if  MissingMA_0==1
replace Imotherpostcompeduc_0_1 =. if Imotherpostcompeduc_0_99==1
replace gestation_0=. if  MissingGE_0==1
replace birthweight_0=. if  MissingBW_0==1
replace kidshh_7=. if  MissingKH_7==1
replace roomsinhouse_7=. if MissingRH_7==1
replace kidshh_11=. if  MissingKH_1==1
replace Ifinancialdiff_11_1=. if  Ifinancialdiff_11_99==1
replace Ifatherpostcompeduc_7_1 =. if Ifatherpostcompeduc_7_99==1
replace fatherageleftsch_7 =. if Ifatherpostcompeduc_7_99==1
replace Ifinancialdiff_7_1 =. if Ifinancialdiff_7_99==1
replace Imumwork_11_1 =. if Imumwork_11_99==1
replace roomsinhouse_11 =. if Ifinancialdiff_11_99==1
replace IFSM_11_1=. if IFSM_11_99==1
replace Pupattendance_7=. if MissingPupattendance_7==1
replace ESN_7=. if  MissingESN_7 ==1
replace NativeEnglish_7=. if MissingNativeEnglish_7==1
replace StateSch_7=. if MissingStateSch_7 ==1
replace SchSize_7=. if MissingSchSize_7==1
replace PTA=. if MissingPTA==1
replace classsize_7=. if Missingclasssize_7 ==1
replace SchAbility_7=. if MissingSchAbility_7 ==1
replace pNkidsParTea=. if MissingpNkidsParTea==1
replace pNDadSC13N=. if MissingDads==1
replace pNDadSC3M4 =. if MissingDads==1
replace pNDadSC5=. if MissingDads==1 
replace pNDadUK=. if MissingDads==1 
replace pNkidsParTea=. if MissingpNkidsParTea==1 
replace Pupattendance_11=. if  MissingPupattendance_11 ==1 
replace ESN_11=. if MissingESN_11 ==1 
replace NativeEnglish_11=. if  MissingNativeEnglish_11==1 
replace StateSch_11=. if MissingStateSch_11==1 
replace SchSize_11=. if MissingSchSize_11==1
replace classsize_11=. if Missingclasssize_11==1
replace SchAbility_11=. if MissingSchAbility_11==1 


foreach var in Iparentwanted_7 Iparentwanted_11  Imumwklastyr_11{
	forvalues j=0/2{
		replace  `var'_`j'=. if  `var'_99==1
	}
	drop  `var'_99
}

foreach var in Imotherread Ifatherread Imotheroutings Ifatheroutings Idadsrole Imotherworkb4sch Imotherworkafsch Ibreatfed{
	forvalues j=2/4{
		su `var'_7_`j'
		replace  `var'_7_`j'=. if  `var'_7_99==1
	}
	drop  `var'_7_99
}



foreach j of numlist 2/4 6{
	replace Ihousetenure_7_`j'=. if Ihousetenure_7_99==1
}
drop Ihousetenure_7_99

foreach var in Imotheroutings Ifatheroutings Idadsrole{
	forvalues j=1/3{
		su `var'_11_`j'
		replace  `var'_11_`j'=. if  `var'_11_99==1
	}
	drop  `var'_11_99
}


foreach j of numlist 1 3 5 6{
	replace ISCmum_11_`j'=. if ISCmum_11_99==1
}
drop ISCmum_11_99

foreach j of numlist 1 2 3 6{
	replace Ihousetenure_11_`j'=. if Ihousetenure_11_99==1
}
drop Ihousetenure_11_99



foreach var in Ispeech_7 Ihearing_7 Ivision_7 Ispeech_11 Ihearing_11 Ivision_11{
	forvalues j=2/2{
		replace  `var'_`j'=. if  `var'_99==1
	}
	drop  `var'_99
}


* remove observations with missing values
forvalues j=1/11{
	replace  Imotherstopped_0_`j'=. if Imotherstopped_0_99==1
}
drop Imotherstopped_0_99

forvalues j=1/5{
	replace Imotherjob_0_`j'=. if Imotherjob_0_99==1
}
drop Imotherjob_0_99


forvalues j=1/5{
	replace Imotherfsc_0_`j'=. if Imotherfsc_0_99==1
}
drop Imotherfsc_0_99

replace Imothersmoke_0_1 =. if Imothersmoke_0_99==1

replace parity_0=. if MissingMP_0==1
replace fatherage_0=. if  MissingFA_0==1

forvalues j=1/5{
	replace Ifathersc_0_`j'=. if (Ifathersc_0_6==1|Ifathersc_0_7==1)
}
drop Ifathersc_0_6 Ifathersc_0_7 


forvalues j=1/6{
	replace Ifathersc_7_`j'=. if (Ifathersc_7_7==1|Ifathersc_7_99==1)
}
drop Ifathersc_7_7 Ifathersc_7_99 


forvalues j=1/6{
	replace Ifathersc_11_`j'=. if (Ifathersc_11_7==1|Ifathersc_11_99==1)
}
drop Ifathersc_11_7 Ifathersc_11_99 


* drop missing indicators
drop Imotherjob_0_5
drop ISCmum_11_6

* standardize
su tevalmr
replace tevalmr=(tevalmr-r(mean))/r(sd)

* slice and dice

global Controls1  motherage_0  Imotherpostcompeduc_0_1  Iregion_0* Imotherstopped_0_*  Imotherjob_0* Imotherfsc_0* Imothersmoke_0_1 parity_0 fatherage_0  

global Controls2 Ifathersc_0* gestation_0 Male birthweight_0 kidshh_7 Iparentwanted_7* Imotherread_7* Imotheroutings_7*  Ifatherread_7* Ifatheroutings_7* Idadsrole_7*  Ifathersc_7* Ifatherpostcompeduc_7_1 fatherageleftsch_7

global Controls3   Imotherworkb4sch_7* Imotherworkafsch_7* Ihousetenure_7* roomsinhouse_7  Ifinancialdiff_7_1  Ibreatfed_7* kidshh_11 Iparentwanted_11*  Imotheroutings_11* Ifatheroutings_11* Idadsrole_11* Ifathersc_11* 

global Controls4  ISCmum_11* Imumwork_11_1   Imumwklastyr_11_2 Imumwklastyr_11_1 Imumwklastyr_11_0 Ihousetenure_11* roomsinhouse_11  Ifinancialdiff_11_1 IFSM_11_1 Ispeech_7* Ihearing_7* Ivision_7*  Ispeech_11* Ihearing_11* Ivision_11* 

global Controls5  Pupattendance_7  ESN_7 NativeEnglish_7 StateSch_7 SchSize_7 PTA classsize_7 SchAbility_7  pNDadSC13N pNDadSC3M4 pNDadSC5 pNkidsParTea Pupattendance_11 ESN_11  NativeEnglish_11 StateSch_11 SchSize_11 classsize_11 SchAbility_11

local  ControlsAll "Controls1 Controls2 Controls3 Controls4 Controls5"


local prehead "\begin{tabular}{p{7.2cm} d{3.2} d{3.2} d{3.2}} \hline \hline &\multicolumn{1}{c}{ \multirow{2}{*}{Creativity}} &\multicolumn{1}{c}{Cognitive ability} &\multicolumn{1}{c}{Teacher evaluation}  \\ & & \multicolumn{1}{c}{(from test scores)} & \multicolumn{1}{c}{of cognitive ability} \\ \cline{2-4} & \\[-.75em] "

local postfoot " & \\[-.7em] \hline \hline \end{tabular}"


foreach n of local ControlsAll{

	mat C=J(1,3,.)
	foreach var of varlist $`n'{
		corr  creativityt_psd_7 $TestC tevalmr `var'
		mat c=r(C)
		mat c=c[4,1..3]
		mat C=C\c
	}

	mat C=C[2...,1...]
	esttab matrix(C, fmt(%8.3f)) using results/Tables/CorrDemog`n'.tex, replace ///
		nomtitle label  nonumber noobs alignment(S) frag collabels(, none) posthead(`prehead') ///
		`controllabels' postfoot(`postfoot')
}















